
function delayer(action, delay = 600) {
    let timer = -1;
    return nv => {
        clearTimeout(timer);
        timer = setTimeout(() => {
            action(nv);
        }, delay);
    };
}
var vm = new Vue({
    el: '#app',
    data() {
        //这里存放数据
        return {
            editableTabsValue: '1',
            editableTabs: [{
                title: '笔记 1',
                name: '1',
                content: ''
            }, {
                title: '笔记 2',
                name: '2',
                content: ''
            }],
            tabIndex: 1
        }
    },
    //监听属性 类似于data概念
    computed: {},
    //监控data中的数据变化
    watch: {
        editableTabs: {
            handler(newValue, oldValue) {
                console.log(`index.js 26`, newValue);
                localStorage.setItem('noteData', JSON.stringify(newValue))
            },
            deep: true
        }
    },
    //方法集合
    methods: {
        clearStorage() {
            this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                localStorage.removeItem("noteData");
                this.editableTabs = [{
                    title: '笔记 1',
                    name: '1',
                    content: ''
                }, {
                    title: '笔记 2',
                    name: '2',
                    content: ''
                }]
                this.$message({
                    type: 'success',
                    message: '清理成功!'
                });
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消删除'
                });
            });
        },
        addTab(targetName) {
            let newTabName = ++this.tabIndex + '';
            this.editableTabs.push({
                title: '笔记 ' + (this.editableTabs.length + 1),
                name: newTabName,
                content: ''
            });
            this.editableTabsValue = newTabName;
        },
        removeTab(targetName) {
            let tabs = this.editableTabs;
            let activeName = this.editableTabsValue;
            if (activeName === targetName) {
                tabs.forEach((tab, index) => {
                    if (tab.name === targetName) {
                        let nextTab = tabs[index + 1] || tabs[index - 1];
                        if (nextTab) {
                            activeName = nextTab.name;
                        }
                    }
                });
            }

            this.editableTabsValue = activeName;
            this.editableTabs = tabs.filter(tab => tab.name !== targetName);
        }
    },

    //生命周期 - 创建完成（可以访问当前this实例）
    created() {
        // this.getSearch()
        let localData = JSON.parse(localStorage.getItem('noteData'))
        if (localData) {
            this.editableTabs = localData
        }
    },
    //生命周期 - 挂载完成（可以访问DOM元素）
    mounted() {

    },
});
